<?php

/**
 * This is the model class for table "tbl_dispatchequipment".
 *
 * The followings are the available columns in table 'tbl_dispatchequipment':
 * @property integer $id
 * @property string $dispatch
 * @property integer $counter
 * @property string $equipmentid
 * @property string $notes
 */
class Dispatchequipment extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Dispatchequipment the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_dispatchequipment';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('dispatch, counter, equipmentid', 'required'),
		
			array('dispatch, equipmentid', 'length', 'max'=>15),
			array('notes', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, dispatch, counter, equipmentid, notes', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    'equipment'=>array(self::BELONGS_TO,'Equipment','equipmentid')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'dispatch' => 'Dispatch',
			'counter' => 'Counter',
			'equipmentid' => 'Equipment',
			'notes' => 'Notes',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('dispatch',$this->dispatch,true);
		$criteria->compare('counter',$this->counter);
		$criteria->compare('equipmentid',$this->equipmentid,true);
		$criteria->compare('notes',$this->notes,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        public function getEquipmentList($dispatch=NULL){
            if($dispatch==NULL) $dispatch=$this->dispatch;
            $dispatch=Yii::app()->db->createCommand('select locno,custno from tbl_dispatchdetails where dispatch=:dispatch')->bindValue('dispatch',$dispatch)->queryRow();
            $rs=array();
            if($dispatch!=false){
                $equipment=Yii::app()->db->createCommand('select * from tbl_equipment where custno=:custno AND locno=:locno')->bindValues(array(
                    'custno'=>$dispatch['custno'],
                    'locno'=>$dispatch['locno']
                ))->queryAll();
                
                if(count($equipment)>0){
                    foreach($equipment as $i=>$item){
                     
                        $rs[$item['id']]=$item['model'];
                    }
                }
            }
            return $rs;
        }
        
        public function getEquipmentName(){
            $equipment=Yii::app()->db->createCommand('select * from tbl_equipment where id=:id')->bindValue('id', $this->equipmentid)->queryRow();
            if($equipment!=NULL){
                return $equipment['model'];
            }
            return NULL;
        }
        
        public function getNextCounter(){
            if($this->isNewRecord){
                $num = Yii::app()->db->createCommand('select max(counter) from tbl_dispatchequipment where dispatch=:dispatch')
                        ->bindValues(array(
                            'dispatch'=>$this->dispatch
                        ))->queryScalar();
                $this->counter = str_pad($num + 1, 3, '0', STR_PAD_LEFT);
            }else{
                return $this->counter;
            }
        }
}